package com.rocksea.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.rocksea.app.R;
import com.rocksea.app.dialog.CommitInspectionDialog;
import com.rocksea.app.entry.CommitBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.rocksea.app.dialog.CommitInspectionDialog.MODE_ALLUNPASS;
import static com.rocksea.app.dialog.CommitInspectionDialog.MODE_OVERPASS;
import static com.rocksea.app.dialog.CommitInspectionDialog.MODE_PASS;

/**
 * Created by 吴志华 on 2017/6/1.
 *
 */

public class PileAdapter extends BaseAdapter
{
    private List<CommitBean> list;
    private Context mContext;
    private final Map<Integer, Boolean> isCheckeds;
    private int mode;
    private CommitInspectionDialog.OnPileErrorMessage onPileErrorMessage;

    public PileAdapter(List<CommitBean> list, Context mContext, int mode, CommitInspectionDialog.OnPileErrorMessage onPileErrorMessage)
    {
        this.onPileErrorMessage = onPileErrorMessage;
        this.list = list;
        this.mContext = mContext;
        this.mode = mode;
        isCheckeds = new TreeMap<>();
        for (int i = 0; i < list.size(); i++)
        {
            isCheckeds.put(i,false);
        }
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView==null)
        {
            convertView = View.inflate(mContext, R.layout.checkbox_item_layout,null);
        }
        ImageView errorImageView = (ImageView) convertView.findViewById(R.id.Error_Message);
        errorImageView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                onPileErrorMessage.onPileErrorMessage(String.format(" %s号桩：  /",list.get(position).getPileName()));
            }
        });
        final CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
        mCheckBox.setText(list.get(position).getPileName());
        mCheckBox.setChecked(isCheckeds.get(position));
        mCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                isCheckeds.put(position,mCheckBox.isChecked());
            }
        });
        String pileNo = list.get(position).getPileName();
        if (list.get(position).getIsUpLoad()==1)
        {
            mCheckBox.setText(String.format("桩号：%s(已上传)",pileNo));
            if (mode==MODE_PASS)
                mCheckBox.setChecked(true);
        }
        else
        {
            mCheckBox.setText(String.format("桩号：%s(未上传)",pileNo));
        }

        switch (mode)
        {
            case MODE_PASS:
                mCheckBox.setEnabled(false);
                break;
            case MODE_ALLUNPASS:
                mCheckBox.setEnabled(false);
                mCheckBox.setChecked(true);
                break;
            case MODE_OVERPASS:
                mCheckBox.setEnabled(true);
                break;
        }
        return convertView;

    }
    public List<Integer> getCheckPosition()
    {
        List<Integer> checkedPosition = new ArrayList<>();
        for (int i = 0; i < isCheckeds.size(); i++)
        {
            if (isCheckeds.get(i))
            {
                checkedPosition.add(i);
            }
        }
        return checkedPosition;
    }

    public void chooseAll() {

        for (Map.Entry<Integer, Boolean> integerBooleanEntry : isCheckeds.entrySet()) {
            integerBooleanEntry.setValue(true);

        }
        notifyDataSetChanged();
    }

    public void unChooseAll() {
        for (Map.Entry<Integer, Boolean> integerBooleanEntry : isCheckeds.entrySet()) {
            integerBooleanEntry.setValue(false);

        }
        notifyDataSetChanged();
    }

    public int getUpLoadCounts()
    {
        int counts = 0;
        for (int i = 0; i < list.size(); i++)
        {
            if (list.get(i).getIsUpLoad()==1) {
                counts++;
            }
        }
        return counts;
    }

    public void changeState(int mode)
    {
        this.mode = mode;
        notifyDataSetChanged();
    }

}
